const jwt = require('jsonwebtoken')
const whiteList = [
  '/api/grade',
  '/api/findUsers',
  '/',
  '/api/login',
  '/api/register',
  '/api/blogs/findAll',
  '/api/blogs/page',
  '/api/authCode',
  '/api/pageGrade',
  '/api/updateGrade'
]
module.exports = (options, app) => {
  return async (ctx, next) => {
    console.log(ctx.path, 'path---path')
    if (!whiteList.includes(ctx.path)) {
      const { authorization } = ctx.request.headers;
      try {
        let data = jwt.verify(authorization, app.config.keys)
        console.log('我在中间件中:', ctx.state)
        // 命名空间  
        ctx.state.user = data;
        await next()
      } catch (err) {
        ctx.body = {
          code: -1,
          code: err.message
        }
      }
    } else {
      await next()
    }
  }
}